import { Box, DatePicker2 } from '@alifd/next';
import React, { useEffect, useState } from 'react';
import styles from './Statistics.module.scss';
import { useStore } from '@/hooks';
import { t } from '@lingui/macro';
import dayjs from 'dayjs';
import PieGroup from './PieGroup';
import TopList from './TopList';
import OrdersInfo from './OrdersInfo';

const RangePresetDay = {};
RangePresetDay[t`今天`] = [dayjs(), dayjs()];
RangePresetDay[t`近一周`] = [dayjs().add(-7, 'day'), dayjs()];
RangePresetDay[t`近二周`] = [dayjs().add(-15, 'day'), dayjs()];

const RangePresetMonth = {};
// RangePresetMonth[t`本月`] = [dayjs().add(0, 'month'), dayjs()];
RangePresetMonth[t`近一月`] = [dayjs().add(-1, 'month'), dayjs()];
RangePresetMonth[t`近二月`] = [dayjs().add(-2, 'month'), dayjs()];

export default function Statistics() {
  const { dashboardStore: store, ruleStore } = useStore();
  const [data, setData] = useState(null);
  useEffect(() => {
    (async () => {
      const res = await store.search({
        search: { groupIds: [ruleStore._group_id] },
        pg: { size: 999, page: 0, sort: ['crDate;desc'] },
      });
      const obj = {
        issuePointsTotal: 0,
        takePointsTotal: 0,
        issueOrderCount: 0,
        takeOrderCount: 0,
      };
      for (let i = 0; i < res.length; i++) {
        const item = res[i];
        const {
          initPoints,
          summary,
          issueOrderCount,
          takeOrderCount,
        } = item;
        obj.issuePointsTotal += Number(initPoints);
        obj.takePointsTotal += Number(summary);
        obj.issueOrderCount += Number(issueOrderCount);
        obj.takeOrderCount += Number(takeOrderCount);
      }
      setData(obj);
    })();
  }, [ruleStore._group_id]);

  if (!data) return null;

  return (
    <div className={styles.statistics}>
      <TopList />
      <PieGroup />
      <OrdersInfo />
    </div>
  );
}
